package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;


/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBaseService;


    /**
     * <p>
     * 分页查询课程下教师信息
     * </p>
     *
     * @param courseBaseId 课程id
     * @return PageVO
     */
    @Override
    public List<CourseTeacherDTO> queryCourseTeacherList(Long courseBaseId, Long companyId) {

        if (ObjectUtils.isEmpty(courseBaseId)) {
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        CourseBase byId = courseBaseService.getById(courseBaseId);
        if (ObjectUtils.isEmpty(byId)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        } else {
            if (!ObjectUtils.nullSafeEquals(companyId, byId.getCompanyId())) {
                ExceptionCast.cast(CommonErrorCode.E_100108);
            }
        }

        int pageSize = PageRequestParams.DEFAULT_PAGE_SIZE;
        int pageNo = PageRequestParams.DEFAULT_PAGE_NUM.intValue();

        Page<CourseTeacher> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<CourseTeacher> queryWrapper = Wrappers.lambdaQuery(CourseTeacher.class)
                .eq(CourseTeacher::getCourseId, courseBaseId);

        Page<CourseTeacher> teacherPage = this.page(page, queryWrapper);
        List<CourseTeacher> records = Collections.emptyList();
        if (CollectionUtils.isNotEmpty(teacherPage.getRecords())) {
            records = teacherPage.getRecords();
        }

        return CourseTeacherConvert.INSTANCE.courseTeacherToDTOList(records);
    }

    @Override
    @Transactional
    public CourseTeacherDTO createOrModifyCourseTeacher(CourseTeacherDTO courseTeacherDTO, Long companyId) {

        Long courseId = courseTeacherDTO.getCourseId();
        if (ObjectUtils.isEmpty(courseId)) {

            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        String photograph = courseTeacherDTO.getPhotograph();
        String position = courseTeacherDTO.getPosition();
        String teacherName = courseTeacherDTO.getTeacherName();


        if (StringUtils.isBlank(photograph) ||
                StringUtils.isBlank(position) ||
                StringUtils.isBlank(teacherName)) {
            ExceptionCast.cast(ContentErrorCode.E_120504);
        }

        CourseBase courseBase = courseBaseService.getById(courseId);


        if (!ObjectUtils.nullSafeEquals(courseBase.getCompanyId(), companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }


        Long courseTeacherId = courseTeacherDTO.getCourseTeacherId();

        CourseTeacher courseTeacher = this.getById(courseTeacherId);


        if (ObjectUtils.isEmpty(courseTeacher)) {
            courseTeacher = createCourseTeacher(courseTeacherDTO);
        } else {
            courseTeacher = modifyCourseTeacher(courseTeacherDTO);
        }

        return CourseTeacherConvert.INSTANCE.courseTeacherToDTO(courseTeacher);

    }

    /**
     * <p>
     * 删除课程教师
     * </p>
     *
     * @param courseBaseId    课程id
     * @param courseTeacherId 课程教师id
     * @return boolean
     */
    @Override
    @Transactional
    public boolean removeCourseTeacher(Long courseBaseId, Long courseTeacherId) {

        if (ObjectUtils.isEmpty(courseBaseId) || ObjectUtils.isEmpty(courseTeacherId)) {
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        //查询要删除的教师是否存在
        LambdaQueryWrapper<CourseTeacher> queryWrapper = Wrappers.lambdaQuery(CourseTeacher.class)
                .eq(CourseTeacher::getCourseId, courseBaseId)
                .eq(CourseTeacher::getId, courseTeacherId);
        CourseTeacher courseTeacher = getOne(queryWrapper);
        if (ObjectUtils.isEmpty(courseTeacher)) {
            ExceptionCast.cast(ContentErrorCode.E_120501);
        }
        boolean remove = this.removeById(courseTeacher);
        if (!remove) {
            ExceptionCast.cast(ContentErrorCode.E_120501);
        }
        return true;
    }

    private CourseTeacher modifyCourseTeacher(CourseTeacherDTO courseTeacherDTO) {

        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dtOToCourseTeacher(courseTeacherDTO);

        boolean update = this.updateById(courseTeacher);
        if (!update) {
            ExceptionCast.cast(ContentErrorCode.E_120501);
        }

        return courseTeacher;
    }

    private CourseTeacher createCourseTeacher(CourseTeacherDTO courseTeacherDTO) {

        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dtOToCourseTeacher(courseTeacherDTO);

        boolean save = this.save(courseTeacher);
        if (!save) {
            ExceptionCast.cast(ContentErrorCode.E_120501);
        }

        return courseTeacher;
    }
}
